'''
    这边我们定义的list主要是和C语言中的链表有些相似，有无顺序和有顺序之分。
    无序列表：链表，需要确保我们相对项的相对定位但不需要维持元素在连续存储器中的定位。
            一旦我们知道第一个项在哪里，就可以按照顺序依次获取接下来的元素

            无序列表最重要的部分是节点，每个节点存储列表项的内容和对下一个节点的引用，还有一些常用方法，如：访问，修改数据和访问下一个引用的常用方法
'''

class Node:
    def __init__(self,initdata):
        self.data = initdata
        self.next = None    # 设置后面没有节点

    def getData(self):
        return self.data

    def getNext(self):
        return self.next

    def setData(self,newdata):
        self.data = newdata

    def setNext(self,newnext):
        self.next = newnext

class UnorderedList:
    def __init__(self):
        self.head = None    # 创建头节点

    def isEmpty(self):  # 头节点为空
        return self.head == None

    def add(self,item):
        temp = Node(item)   # 新建节点
        temp.setNext(self.head) # 节点指向头部
        self.head = temp    # 头节点变成temp了

    def size(self):
        current = self.head
        count = 0
        while current != None:
            count = count + 1
            current = current.getNext()
        return count

    def search(self,item):
        current = self.head
        found = False
        while current != None and not found:
            if current.getData() == item:
                found = True
            else:
                current = current.getNext()
        return found

    def remove(self,item):
        current = self.head
        previous = None
        found = False
        while not found:
            if current.getData() == item:
                found = True
            else:
                previous = current
                current = current.getNext()
        if previous == None:
            self.head = current.getNext()   # 删除的是头部
        else:
            previous.setNext(current.getNext())

if __name__ == '__main__':
    list1 = UnorderedList()
    print(list1.isEmpty())
    list1.add(5)
    print(list1.isEmpty())
    print(list1.size())
    print(list1.search(4))
    list1.remove(5)
    print(list1.isEmpty())
